package com.hospital.pony.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;


import com.google.gson.reflect.TypeToken;
import com.hospital.CreatTopicActivity;
import com.hospital.R;
import com.hospital.pony.base.BaseFragment;
import com.hospital.pony.config.Consts;
import com.hospital.pony.db.TopicObservable;
import com.hospital.pony.model.ResponseEntry;
import com.hospital.pony.model.TopicModel;
import com.hospital.pony.util.ToastUtil;
import com.hospital.pony.adapter.TopicAdapter;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


public class TopicFragment extends BaseFragment implements  Observer {
    // 获取view
    private View rootView;
    // 获取控件
    private ListView mListMessage;
    private List<TopicModel> listMsg = new ArrayList<TopicModel>();
    private ImageView mivCreateMessage;

    // 预加载标志
    private boolean isPrepared;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_news, null);
        initWidget();
        initData();
        return rootView;
    }



    @Override
    public void initWidget() {
        mListMessage = (ListView) rootView.findViewById(R.id.mListMessage);
        mivCreateMessage = (ImageView) rootView.findViewById(R.id.mivCreateMessage);
        mivCreateMessage.setOnClickListener(this);
        mivCreateMessage.setVisibility(View.VISIBLE);
        mListMessage.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            }
        });


    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void initData() {


        mivCreateMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CreatTopicActivity.class);
                getActivity().startActivity(intent);
            }
        });
        listMessage(true);
    }

    private void listMessage(boolean isShow) {
        AjaxParams params = new AjaxParams();
        params.put("action_flag", "listTopic");
        httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultCode, isShow, "正在加载...");
    }


    TopicAdapter planAdapter;

    @Override
    protected void callBackSuccess(ResponseEntry entry, int actionId) {
        super.callBackSuccess(entry, actionId);

        switch (actionId) {



            case Consts.actionId.resultCode:
                if (null != entry.getData() && !TextUtils.isEmpty(entry.getData())) {

                    String jsonMsg = entry.getData().substring(1, entry.getData().length() - 1);
                    if (null != jsonMsg && !TextUtils.isEmpty(jsonMsg)) {
                        listMsg.clear();
                        listMsg = mGson.fromJson(entry.getData(), new TypeToken<List<TopicModel>>() {
                        }.getType());
                        planAdapter = new TopicAdapter(getActivity(), listMsg);
                        mListMessage.setAdapter(planAdapter);
                    }
                }
                break;



        }

    }

    @Override
    protected void callBackAllFailure(String strMsg, int actionId) {
        super.callBackAllFailure(strMsg, actionId);
        ToastUtil.show(getActivity(), strMsg);

    }




    @Override
    public void onResume() {
        super.onResume();

        TopicObservable.getInstance().addObserver(this);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        TopicObservable.getInstance().deleteObserver(this);
    }

    @Override
    public void update(Observable arg0, Object object) {
        listMessage(false);
    }


}
